package br.com.moxeon.control;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.moxeon.model.cash.ThrowOperation;
import br.com.moxeon.model.enums.TypeThrowOperation;
import br.com.vexillum.util.Message;
import br.com.vexillum.util.Return;

@Service
@Scope("prototype")
public class ThrowOperationController extends BaseController<ThrowOperation>{

	public ThrowOperationController() 
	{
		super(ThrowOperation.class);
	}

	public Return executeOperation()
	{
		Return ret = new Return(true);
		entity = new ThrowOperation();
		entity.setTipoOperacao((TypeThrowOperation) getData().get("operation"));
		entity.setOparationDate(new Date());
		return ret;
	}
	
	public Return reverseOperation(){
		Return ret = new Return(true);
		entity.setActive(false); 
		ret.concat(update(entity));
		return ret;
	}
	
	
	public Return listAll()
	{
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE active = true ORDER BY oparationDate Desc";
		data.put("sql", sql);
		return super.searchByHQL();
	}
	
	public Return searchThrowOperation()
	{
		Date throwStart = (Date) data.get("throwDateStart");
		Date throwEnd = (Date) data.get("throwDateEnd");
		Return ret = new Return(true);

		String hql = "From ThrowOperation where active = true ";
		if (throwStart != null && throwEnd != null) {	
			if(throwStart.after(throwEnd)){
				ret.addMessage(new Message(null, messages.getKey("start_date_is_more_than_end_date")));
				ret.setValid(false);
			}else{
				hql += " and oparationDate between '" + SimpleDateFormat.getDateInstance().format(throwStart) + "' and '" + SimpleDateFormat.getDateInstance().format(throwEnd) + "'";		
			}
		}	
		hql += "order by oparationDate Desc";
		data.put("sql", hql);
		ret.concat(searchByHQL());
		return ret;
	}
	

	
}
